"""
@name : tryex
@author : wenyao
@projectname: sanchuanglianxi
"""

##出现异常 程序终止
# print("aaa")
# f = open("test.txt", 'r')
# print("bbb")

#异常类

#nameerror
#print(b)

#除数为0 ZeroDivisionError
#a = 1/0

#语法错误  SyntaxError: invalid syntax
# print("语法错误")
# if 1>0::
#     print("abc")

# 超出索引列范围  IndexError: list index out of range
# a = [1,2,3]
# print(a[3])

#keyerror  KeyError: 'key3'
# a = {"key1":"123", "key2":"456"}
# print(a["key1"] , a.get("key3","test"))

#访问对象未知属性 AttributeError
# import math
#
# math.xxx(4)

#传给函数的参数类型不正确  ValueError
#int("abc")

#异常处理机制 try  except
# print("start.......")
# try:
#     print("test try except")
#     a = 1/0
#     print("test try except 2!!!!")
# except ZeroDivisionError as  e:
#     print("zero error!!!!")
#     print(e)
# print("end........")

#except捕获的错误与触发的错误不一致，程序会捕获不到
#匹配到第一个except就退出
#如果父类异常在最前面，会吞噬所有的子类异常
# try:
#     a = 1/0
#     # b = [1,2,3]
#     # print(b[3])
# except IndexError as ie:
#     print("index error:",ie)
# except ZeroDivisionError as ze:
#     print("zero division error")
# except Exception as e :
#     print("exception error:",e)
# print("end......")

#else 子句  没有异常发生时 执行
# try:
#     #a = 1/0
#      a = "hello world"
# except ZeroDivisionError as msg:
#     print("zero division")
#     print(msg)
# else:
#     print("code ok!")

#finally子句：  不管有没有异常都执行
# try:
#     #a = 1/0
#     b = "python"
#     # c = [1,2,3]
#     # print(c[3])
# except ZeroDivisionError as msg:
#     print("zero division")
# else:
#     print("code ok")
# finally:
#     print("this is finally")
# print("end......")

#raise  手动触发异常
# try:
#     print("raise test")
#     raise ValueError("value error test")
# except ValueError as e:
#     print("xxxxxxxxxxxxx")
#     print(e)

# s = input("请输入学生成绩:")
# try:
#     s1 = int(s)
#     if 0<=s1<=100:
#         print(f"你的成绩是{s1}")
#     else:
#         raise TypeError("分数范围有误")
# except ValueError as e:
#     print("输入有误")
# except TypeError as e:
#     print(e)

#异常嵌套
# def aa():
#     try:
#         a = "hello world"
#         try:
#             int("hello world")
#         except ValueError as e:
#             print("Value Error")
#             return "i am value error"
#         finally:
#             print ("i am finaly error")
#         print("xxxxxxxx")
#         b = [1,2,3]
#         print(b[3])
#     except IndexError as msg:
#         print("index error")
#     finally:
#         print("i am index error")
# b = aa()
# print("=======")
# print(b)

#断言
# a = 1
# b = 2
# assert  a>b , "a必须大于b"
# assert  a<b, "a必须小于b"

#多函数（finally  会造成一个异常丢失）
def func2():
    try:
        int("abc")
    # except ValueError as e:
    #     print(e)
    finally:
        return "return func2"
def func1():
    try:
        func2()
    finally:
        print("func1 finally")

try:
    func1()
finally:
    print("main finally")


